package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbGoodsMapper;
import com.pinyougou.pojo.TbGoods;
import com.pinyougou.pojo.TbGoodsExample;
import com.pinyougou.pojo.TbGoodsExample.Criteria;
import com.pinyougou.sellergoods.service.GoodsService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class GoodsServiceImpl implements GoodsService {

  @Autowired private TbGoodsMapper goodsMapper;

  /** 查询全部 */
  @Override
  public List<TbGoods> findAll() {
    return goodsMapper.selectByExample(null);
  }

  /** 按分页查询 */
  @Override
  public PageResult findPage(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);
    Page<TbGoods> page = (Page<TbGoods>) goodsMapper.selectByExample(null);
    return new PageResult(page.getTotal(), page.getResult());
  }

  /** 增加 */
  @Override
  public void add(TbGoods goods) {
    goodsMapper.insert(goods);
  }

  /** 修改 */
  @Override
  public void update(TbGoods goods) {
    goodsMapper.updateByPrimaryKey(goods);
  }

  /**
   * 根据ID获取实体
   *
   * @param id
   * @return
   */
  @Override
  public TbGoods findOne(Long id) {
    return goodsMapper.selectByPrimaryKey(id);
  }

  /** 批量删除 */
  @Override
  public void delete(Long[] ids) {
    for (Long id : ids) {
      goodsMapper.deleteByPrimaryKey(id);
    }
  }

  @Override
  public PageResult findPage(TbGoods goods, int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize);

    TbGoodsExample example = new TbGoodsExample();
    Criteria criteria = example.createCriteria();

    if (goods != null) {
      if (goods.getSellerId() != null && goods.getSellerId().length() > 0) {
        criteria.andSellerIdLike("%" + goods.getSellerId() + "%");
      }
      if (goods.getGoodsName() != null && goods.getGoodsName().length() > 0) {
        criteria.andGoodsNameLike("%" + goods.getGoodsName() + "%");
      }
      if (goods.getAuditStatus() != null && goods.getAuditStatus().length() > 0) {
        criteria.andAuditStatusLike("%" + goods.getAuditStatus() + "%");
      }
      if (goods.getIsMarketable() != null && goods.getIsMarketable().length() > 0) {
        criteria.andIsMarketableLike("%" + goods.getIsMarketable() + "%");
      }
      if (goods.getCaption() != null && goods.getCaption().length() > 0) {
        criteria.andCaptionLike("%" + goods.getCaption() + "%");
      }
      if (goods.getSmallPic() != null && goods.getSmallPic().length() > 0) {
        criteria.andSmallPicLike("%" + goods.getSmallPic() + "%");
      }
      if (goods.getIsEnableSpec() != null && goods.getIsEnableSpec().length() > 0) {
        criteria.andIsEnableSpecLike("%" + goods.getIsEnableSpec() + "%");
      }
      if (goods.getIsDelete() != null && goods.getIsDelete().length() > 0) {
        criteria.andIsDeleteLike("%" + goods.getIsDelete() + "%");
      }
    }

    Page<TbGoods> page = (Page<TbGoods>) goodsMapper.selectByExample(example);
    return new PageResult(page.getTotal(), page.getResult());
  }

  @Override
  public String SayHelloWorld() {
    return "hello  world ";
  }
}
